package com.lx.web;

import com.lx.entity.User;
import com.lx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class MyUserServlet {
    @Autowired
    private UserService userService;

    //跳转到查询页面
    @RequestMapping("/toUser")
    public String toUser(){
        return "/WEB-INF/jsp/user.jsp";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(){
        return "/WEB-INF/jsp/updateUser.jsp";
    }

    /**
     * 修改用户
     * @param id
     * @param user_name
     * @param password
     * @param real_name
     * @param birthday
     * @param phone
     * @param address
     * @return
     */
    @RequestMapping("/updateUser")
    public String upDateUser(int id, String user_name, String password, String real_name
            , String birthday, String phone, String address, HttpServletRequest request){
        //将String的日期转换为Data类型
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = new User(id, user_name, password, real_name, parse, phone, address);
        boolean b = userService.updateUser(user);
        if (b){
            //将session中的数据信息刷新
            request.getSession().setAttribute("user",user);
            return "/toUser";
        }else {
            System.out.println("修改失败！");
            return "/toUpdateUser";
        }
    }
}
